#include<iostream>
#include<vector>
#include<unordered_map>

using namespace std;

vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
    unordered_map<int, int> hash;
    vector<int> res;
    for (auto n : nums1) {
        hash[n]++;
    }
    for (auto n : nums2) {
        if (hash[n] > 0) {
            hash[n]--;
            res.push_back(n);
        }
    }
    return res;
}

int main()
{
    vector<int> nums1 = { 4,9,5 };
    vector<int> nums2 = { 9,4,9,8,4 };

    vector<int> res = intersect(nums1, nums2);
    for (auto n : res) {
        cout << n << " ";
    }
    cout << endl;

	return 0;
}